home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 / Programming Windows 95.iso / code / CHAP20 / CLASSFAC.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-01  |  3.0 KB  |  119 lines

  1. /*---------------------------------------------
  2.    CLASSFAC.CPP -- OLE Class Factory component
  3.                    (c) Paul Yao, 1996
  4.   ---------------------------------------------*/
  5. #include <windows.h>
  6. #include <initguid.h>
  7. #include "pubmem.h"
  8.  
  9. extern int cObject ;
  10. extern int cLockCount ;
  11.  
  12. //-------------------------------------------------------------------
  13. DClassFactory::DClassFactory ()
  14.      {
  15.      RefCount = 0 ;
  16.      }
  17.  
  18. //-------------------------------------------------------------------
  19. DClassFactory::~DClassFactory ()
  20.      {
  21.      }
  22.  
  23. //-------------------------------------------------------------------
  24. STDMETHODIMP
  25. DClassFactory::QueryInterface (REFIID riid, LPVOID FAR *ppvObj)
  26.      {
  27.      // Init recipient's pointer
  28.      *ppvObj = NULL ;
  29.  
  30.      // If asking for IUnknown, we can provide
  31.      if (riid == IID_IUnknown)
  32.           *ppvObj = (LPUNKNOWN) this ;
  33.  
  34.      // If asking for IClassFactory, we can provide
  35.      if (riid == IID_IClassFactory)
  36.           *ppvObj = (LPCLASSFACTORY) this ;
  37.  
  38.      // Make sure reference count reflects access
  39.      if (*ppvObj == NULL)
  40.           {
  41.           // Interface not supported
  42.           return E_NOINTERFACE ;
  43.           }
  44.      else
  45.           {
  46.           // Interface supported to increment reference count
  47.           ((LPUNKNOWN) *ppvObj)->AddRef () ;
  48.           return S_OK ;
  49.           }
  50.      }
  51.  
  52. //-------------------------------------------------------------------
  53. STDMETHODIMP_ (ULONG) 
  54. DClassFactory::AddRef ()
  55.      {
  56.      return ++RefCount ;
  57.      }
  58.  
  59. //-------------------------------------------------------------------
  60. STDMETHODIMP_ (ULONG) 
  61. DClassFactory::Release ()
  62.      {
  63.      if (0L != --RefCount)
  64.           return RefCount ;
  65.  
  66.      delete this ;
  67.      return 0L ;
  68.      }
  69.  
  70. //-------------------------------------------------------------------
  71. STDMETHODIMP
  72. DClassFactory::CreateInstance (LPUNKNOWN pUnkOuter, REFIID riid,
  73.                                LPVOID FAR *ppvObject)
  74.      {
  75.      // Initialize return pointer
  76.      *ppvObject = NULL ;
  77.  
  78.      // If trying to aggregate, fail
  79.      if (pUnkOuter != NULL) 
  80.           return CLASS_E_NOAGGREGATION ;
  81.  
  82.      // Create memory allocation object
  83.      LPMALLOC pMalloc = CreateAllocator () ;
  84.  
  85.      if (pMalloc == NULL)
  86.           {
  87.           return E_OUTOFMEMORY ;
  88.           }
  89.      else
  90.           {
  91.           // Fetch interface requested by caller
  92.           HRESULT hr = pMalloc->QueryInterface (riid, ppvObject) ;
  93.  
  94.           // Decrement reference count produced by CreateAllocator
  95.           pMalloc->Release () ;
  96.  
  97.           // Increment count of objects
  98.           if (SUCCEEDED (hr))
  99.                ++cObject ;
  100.  
  101.           return hr ;
  102.           }
  103.      }
  104.  
  105. //-------------------------------------------------------------------
  106. STDMETHODIMP
  107. DClassFactory::LockServer (BOOL fLock)
  108.      {
  109.      if (fLock)
  110.           {
  111.           ++cLockCount ;
  112.           }
  113.      else
  114.           {
  115.           --cLockCount ;
  116.           }
  117.      return NOERROR ;
  118.      }
  119.